package com.jhws.business.device.api;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.jhws.business.comm.bo.TblBuildingDimBo;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.device.bo.AllDevs;
import com.jhws.business.device.bo.DevsNumber;
import com.jhws.business.device.bo.DevsNumberOfUnit;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.core.exception.BizException;
import com.jhws.business.comm.service.ITblBuildingDimService;
import com.jhws.business.comm.service.ITblCommDimService;

@Controller
@RequestMapping("deviceInfo")
public class deviceCtrl {

    @Resource
    private ITblCommDimService ICommService;
    @Resource
    private ITblBuildingDimService IBuildingService;

    @RequestMapping("getDeviceNumberOfUnit")
    @ResponseBody
    public JsonRespData getDeviceNumberOfUnit(String Room) throws Exception {
        if (Room.length() != 12)
            throw new BizException("-1", "getDeviceNumberOfUnit", "房号长度不对");
        int deviceNumber = IBuildingService.getUnitDevNumber(Room);
        if (deviceNumber < 0)
            throw new BizException("-1", "getUnitDevNumber", "未查找到数据");
        return new JsonRespData("getDeviceNumberOfUnit", 1, deviceNumber);
    }

    @RequestMapping("getDevNumber")
    @ResponseBody
    public JsonRespData getDevInfoForUser(String Room, String type) throws Exception {
        if (Room.length() != 12 && Room.length() != 4)
            throw new BizException("-1", "getDevNumber", "房号长度不对");

        String comm = Room.substring(0, 4);
        String where = "where 1=1 and comm_code = '" + comm + "'";
        List<TblCommDimBo> comms = ICommService.findTblCommDimByWhere(where);

        if (type.equals("owner")) {
            DevsNumber info = new DevsNumber();
            int deviceNumber = IBuildingService.getUnitDevNumber(Room);
            if (deviceNumber < 0)
                throw new BizException("-1", "getUnitDevNumber", "未查找到数据");
            info.setDevNumerOfUnit(deviceNumber);
            info.setGateDevNumber(comms.get(0).getGateDevNum());
            info.setManageDevNumber(comms.get(0).getManageDevNum());
            return new JsonRespData("getDevNumberForOwner", 1, info);
        } else if (type.equals("manager")) {
            List<TblBuildingDimBo> bos = IBuildingService.findTblBuildingDimByWhere(where);
            if (bos.size() == 0)
                throw new BizException("-1", "getDevNumberForManager", "未查找到单元数据");
            List<DevsNumberOfUnit> unitDevsList = new ArrayList<DevsNumberOfUnit>();
            for (int i = 0; i < bos.size(); i++) {
                DevsNumberOfUnit item = new DevsNumberOfUnit();
                item.setAreaName(bos.get(i).getAreaName());
                item.setBuildingName(bos.get(i).getBuildingName());
                item.setDevNumber(bos.get(i).getDevNumber());
                unitDevsList.add(item);
            }
            AllDevs alldevs = new AllDevs();
            alldevs.setDevsList(unitDevsList);
            alldevs.setGateDevNumber(comms.get(0).getGateDevNum());
            return new JsonRespData("getDevNumberForManager", 1, alldevs);
        } else if (type.equals("outdoor")) {
            DevsNumber info = new DevsNumber();
            info.setGateDevNumber(comms.get(0).getGateDevNum());
            info.setManageDevNumber(comms.get(0).getManageDevNum());
            return new JsonRespData("getDevNumberForOutdoor", 1, info);
        } else {
            throw new BizException("-1", "getDevNumber", "unKnown type");
        }
    }
}